<?php
/**
 * Users controller class
 *
 */
class RegionsController extends Admin_Controller
{
    private $_table; // main table for working

	public function init()
    {
    	parent::init();
        $this->_table = new Admin_Table_Regions();        
        $this->setActiveCat(131);
    }

    /**
     * index action method
     *
     */
    public function indexAction()
    {
    	$this->page_type = 'list';	

    	$lclm = new Llib_List($this->_request, $this->_sess);
    	$lclm->AddParams(array('checkboxes' => true)); 
    	$lclm->AddColumn('Название', 'name', 'regions.name_ru', array('width' => '45%', 'sort' => 1));    	    	
    	$lclm->AddColumn('Страна', 'country','countries.name_ru', array('width' => '40%', 'sort' => 1));    	    	

    	$lclm->AddData($this->_table->GetDataList($lclm->page, $lclm->s, $lclm->pager_count));

        $this->startContent();
	        $this->view->xmlWriter->startElement("Main");	        

	        $this->view->xmlWriter->startElement("Actions");	        
	        $this->view->xmlWriter->endElement();

	        $this->view->assign('List', $lclm->GetList());
	        $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }

    public function addAction()
    {
    	$form = new Llib_Form($this->_request);

        $d = array();
    	if ($this->_request->isPost()) 
        {
			$d = $form->GetPostedData();
        	if($this->_table->insert($d))
        	{
        	    $d = array();
        		$form->SetSuccess();        	
        	}
        	else $form->SetError($this->_table->GetError());        	
        } 

        $d['id_country'] = $this->table_cities->GetCountriesList(@$d['id_country']);

        $form->AddData($d);       

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('Title', 'Добавить');
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }
    
    public function editAction()
    {
    	$id = $this->_request->getParam('id');
    	if(!($id > 0)) $this->_redirect('/error/e404');

    	$form = new Llib_Form($this->_request);

        if ($this->_request->isPost()) 
        {
			$d = $form->GetPostedData();
        	if($this->_table->update($d, $id)) 
        	{
        		$d = array();
        		$form->SetSuccess();        	
        	}
        	else $form->SetError($this->_table->GetError());        	
        }
        
        $data = $this->_table->GetDataRow($id); 
        $data['id_country'] = $this->table_cities->GetCountriesList($data['id_country']);       
        
        $form->AddData($data);       

    	$this->startContent();
	        $this->view->xmlWriter->startElement("Main");
	        $this->view->assign('Title', 'Редактировать');
	        $this->view->assign($form->GetFrom());
		    $this->view->xmlWriter->endElement();
	    $this->view->xmlWriter->endElement();
    }    

    public function removeAction()
    {
    	$form = new Llib_Form($this->_request);
    	$d = $form->GetPostedData();
    	if(count($d['selected_id']) > 0) $this->_table->remove($d['selected_id'], $this->_user->id);
    	$this->_redirect('/'.$this->controller);    	
    }

}
